<?php
    class Admincp_LocationController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_locale;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->view->headTitle('Quản lý địa điểm');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Location();
            $this->_locale = 'vi';
        }
        
        public function preDispatch()
		{
		    $auth = Zend_Auth::getInstance();
		    if(!$auth->hasIdentity()) {
		        if($this->_request->getActionName() != 'login')
		            $this->_redirect('admincp/index/login');
		    }
		}
        
        public function indexAction()
        {
            if($_POST['add']) {
                $this->_redirect('admincp/location/create');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteMultiLocation($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listLocation($this->_locale));
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction()
        {
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            $data = $this->_model->listForMenu($this->_locale);
            foreach($data as $value) {
                $menuData['items'][$value['id']] = $value;
                $menuData['parent'][$value['parent']][] = $value['id'];
            }
            $this->view->parent = $menuData;
            
            if($this->_request->isPost()) {
                $strTrim = new Zend_Filter_StringTrim();
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $parent = (int) $this->_arrParam['parent'];
                $name['vi'] = $strTrim->filter(strip_tags($this->_arrParam['name-vi']));
                $name['zh'] = $strTrim->filter(strip_tags($this->_arrParam['name-zh']));
                $name['en'] = $strTrim->filter(strip_tags($this->_arrParam['name-en']));
                $name['trv'] = $strTrim->filter(strip_tags($this->_arrParam['name-trv']));
                $data = array(
                    'parent'=>$parent,
                    'status'=>1,
                );
                $location_id = $this->_model->addLocation($data);
                foreach($name as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'location_id'=>$location_id,
                                'language_id'=>$lang['id'],
                                'name'=>$value,
                                'name_plaintext'=>$strFlex->filter(strtolower($value)),
                            );
                            $this->_model->addLocationLanguage($data);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/location');
            }
        }
        
        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            $this->view->id = $id;
            $this->view->data = $this->_model->getLocationById($id);
            
            $dataLocation = $this->_model->getLocationByLocale($id, $this->_locale);
            $menuData['currentParent'] = $dataLocation['parent'];
            $menuData['currentId'] = $id;
            $data = $this->_model->listForMenu($this->_locale);
            foreach($data as $value) {
                $menuData['items'][$value['id']] = $value;
                $menuData['parent'][$value['parent']][] = $value['id'];
            }
            $this->view->parent = $menuData;
            
            if($this->_request->isPost()) {
                $strTrim = new Zend_Filter_StringTrim();
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $language_id = (int) $this->_arrParam['language'];
                $name['vi'] = $strTrim->filter(strip_tags($this->_arrParam['name-vi']));
                $name['zh'] = $strTrim->filter(strip_tags($this->_arrParam['name-zh']));
                $name['en'] = $strTrim->filter(strip_tags($this->_arrParam['name-en']));
                $name['trv'] = $strTrim->filter(strip_tags($this->_arrParam['name-trv']));
                $parent = (int) $this->_arrParam['parent'];
                $dataLocation = array(
                    'parent'=>$parent,
                );
                $this->_model->updateLocation($dataLocation, $id);
                foreach($name as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'language_id'=>$lang['id'],
                                'name'=>$value,
                                'name_plaintext'=>$strFlex->filter(strtolower($value)),
                            );
                            $this->_model->updateLocationLanguage($data, $id);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/location');
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->_model->deleteLocation($id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
            $status = (int) $this->_arrParam['status'];
            $status = ($status == 1) ? 2 : 1;
            $data = array(
                'status'=>$status,
            );
            $this->_model->updateLocation($data, $id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function searchAction()
        {
            $strTrim = new Zend_Filter_StringTrim();
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
            $name = $strTrim->filter(strip_tags($this->_arrParam['name']));
            $status = (int) $this->_arrParam['status'];
            $this->view->keyword = array(
                'name'=>$name,
                'status'=>$status,
            );
            $data = array(
                'name'=>(string) '%'.str_replace(' ','%',$strFlex->filter($name)).'%',
                'status'=>$status,
            );
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchLocation($data, $this->_locale));
		    $paginator = new Zend_Paginator($adapter);
		    $paginator->setItemCountPerPage(40);
		    $paginator->setPageRange(5);
		    $currentPage = $this->_request->getParam('page', 1);
		    $paginator->setCurrentPageNumber($currentPage);
		    $this->view->data = $paginator;
		    $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function checkAction()
        {
            $this->_helper->layout()->disableLayout();
            $id = (int) $this->_arrParam['id'];
            $name = $this->_arrParam['name'];
            if($this->_model->checkLocation($name, $id) > 0) {
                echo 'Tên địa điểm đã tồn tại';
            }
            else {
                echo '';
            }
        }
    }
